/**
 * 
 */
package org.dragonfire.dasm.directives;

import java.util.ArrayList;
import java.util.HashSet;

import org.dragonfire.dasm.Command;
import org.dragonfire.dasm.Listing;
import org.dragonfire.dasm.DirectiveManager.Directive;
import org.dragonfire.file.BufferedFile;

/**
 * Includes a file as assembler source.<br />
 * <br />
 * Format:<br />
 * <code>#INCLUDESRC <i>&lt;arg1&gt;</i>[,<i>&lt;arg2&gt;</i>[,<i>...</i>]]</code><br />
 * <br />
 * Parameters:<br />
 * <table>
 * <tr>
 * <td><code>arg1</code></td>
 * <td> (String) The path & name of the file to include.</td>
 * </tr>
 * </table><br />
 * Errors:<br />
 * <table>
 * <tr>
 * <td><code>ERROR_STRING_REQUIRED</code></td>
 * <td>Thrown if <code>arg1</code> is not a string</td>
 * </tr>
 * <tr>
 * <td><code>ERROR_FILE_NOT_FOUND</code></td>
 * <td>Thrown if <code>arg1</code> is not a valid path and filename or the
 * file does not exist</td>
 * </tr>
 * </table>
 * 
 * @author Darth Android
 * @see org.dragonfire.dasm.directives.Include
 */
public class Includesrc extends Directive {

	private HashSet<String> includedFiles = new HashSet<String>();

	/**
	 * {@inheritDoc}
	 */
	@Override
	public ArrayList<Listing> pass1(Command c) {
		ArrayList<Listing> list = new ArrayList<Listing>();
		for (int i = 0; i < c.getParams().length; i++) {
			BufferedFile file = BufferedFile.openBufferedFile(evalString(c.getParam(i)));
			includedFiles.add(file.getFullName().toLowerCase());
			list.addAll(getPreprocessor().preprocessFile(file));
		}
		return list;
	}

	public boolean isFileIncluded(String name) {
		return includedFiles.contains(name.toLowerCase());
	}
}
